********************************************************************************
** 	TITLE:		us2016_anes_contact_networks                                  ** 	
**  AUTHOR:	    Philippe Mongrain                                             **
**	DATA:       anes_timeseries_2016                                          **
**  DATE:	    October 2022 						                          **	
**	VERSION:	Stata 16					                                  **	
********************************************************************************

* Version control

version 16.0

* Open log file

capture log close       			  			              
log using "us2016_anes_contact_networks", replace

* Open the dataset

use "anes_timeseries_2016.dta", clear


**************************
** STATE-LEVEL OUTCOMES **
**************************

* Note: 1 = Democrat, 2 = Republican

gen statewinner = .

replace statewinner = 2 if V163001a == 1
replace statewinner = 2 if V163001a == 2
replace statewinner = 2 if V163001a == 4
replace statewinner = 2 if V163001a == 5
replace statewinner = 1 if V163001a == 6
replace statewinner = 1 if V163001a == 8
replace statewinner = 1 if V163001a == 9
replace statewinner = 1 if V163001a == 10
replace statewinner = 1 if V163001a == 11
replace statewinner = 2 if V163001a == 12
replace statewinner = 2 if V163001a == 13
replace statewinner = 1 if V163001a == 15
replace statewinner = 2 if V163001a == 16
replace statewinner = 1 if V163001a == 17
replace statewinner = 2 if V163001a == 18
replace statewinner = 2 if V163001a == 19
replace statewinner = 2 if V163001a == 20
replace statewinner = 2 if V163001a == 21
replace statewinner = 2 if V163001a == 22
replace statewinner = 1 if V163001a == 23
replace statewinner = 1 if V163001a == 24
replace statewinner = 1 if V163001a == 25
replace statewinner = 2 if V163001a == 26
replace statewinner = 1 if V163001a == 27
replace statewinner = 2 if V163001a == 28
replace statewinner = 2 if V163001a == 29
replace statewinner = 2 if V163001a == 30
replace statewinner = 2 if V163001a == 31
replace statewinner = 1 if V163001a == 32
replace statewinner = 1 if V163001a == 33
replace statewinner = 1 if V163001a == 34
replace statewinner = 1 if V163001a == 35
replace statewinner = 1 if V163001a == 36
replace statewinner = 2 if V163001a == 37
replace statewinner = 2 if V163001a == 38
replace statewinner = 2 if V163001a == 39
replace statewinner = 2 if V163001a == 40
replace statewinner = 1 if V163001a == 41
replace statewinner = 2 if V163001a == 42
replace statewinner = 1 if V163001a == 44
replace statewinner = 2 if V163001a == 45
replace statewinner = 2 if V163001a == 46
replace statewinner = 2 if V163001a == 47
replace statewinner = 2 if V163001a == 48
replace statewinner = 2 if V163001a == 49
replace statewinner = 1 if V163001a == 50
replace statewinner = 1 if V163001a == 51
replace statewinner = 1 if V163001a == 53
replace statewinner = 2 if V163001a == 54
replace statewinner = 2 if V163001a == 55
replace statewinner = 2 if V163001a == 56


*********************
** COMPETITIVENESS **
*********************

* Margin of victory by state (absolute difference between Democratic and Republican candidates)

gen margin = .

replace margin = 27.72515 if V163001a == 1
replace margin = 14.73064 if V163001a == 2
replace margin = 3.545593 if V163001a == 4
replace margin = 26.92097 if V163001a == 5
replace margin = 30.10929 if V163001a == 6
replace margin = 4.905582 if V163001a == 8
replace margin = 13.63939 if V163001a == 9
replace margin = 11.43051 if V163001a == 10
replace margin = 86.41351 if V163001a == 11
replace margin = 1.198627 if V163001a == 12
replace margin = 5.13134 if V163001a == 13
replace margin = 31.54109 if V163001a == 15
replace margin = 31.76942 if V163001a == 16
replace margin = 17.06361 if V163001a == 17
replace margin = 19.16519 if V163001a == 18
replace margin = 9.409546 if V163001a == 19
replace margin = 20.60221 if V163001a == 20
replace margin = 29.83745 if V163001a == 21
replace margin = 19.63912 if V163001a == 22
replace margin = 2.868538 if V163001a == 23
replace margin = 26.41644 if V163001a == 24
replace margin = 26.76386 if V163001a == 25
replace margin = .2230301 if V163001a == 26
replace margin = 1.51984 if V163001a == 27
replace margin = 17.82625 if V163001a == 28
replace margin = 18.63712 if V163001a == 29
replace margin = 20.53097 if V163001a == 30
replace margin = 25.0486 if V163001a == 31
replace margin = 2.41713 if V163001a == 32
replace margin = .3675957 if V163001a == 33
replace margin = 14.1027 if V163001a == 34
replace margin = 8.213131 if V163001a == 35
replace margin = 23.74554 if V163001a == 36
replace margin = 3.655228 if V163001a == 37
replace margin = 35.72889 if V163001a == 38
replace margin = 8.129574 if V163001a == 39
replace margin = 36.39119 if V163001a == 40
replace margin = 10.97782 if V163001a == 41
replace margin = .7242699 if V163001a == 42
replace margin = 15.50855 if V163001a == 44
replace margin = 14.26591 if V163001a == 45
replace margin = 29.79332 if V163001a == 46
replace margin = 26.0057 if V163001a == 47
replace margin = 8.999428 if V163001a == 48
replace margin = 18.07955 if V163001a == 49
replace margin = 25.96336 if V163001a == 50
replace margin = 5.323708 if V163001a == 51
replace margin = 15.706 if V163001a == 53
replace margin = 42.15365 if V163001a == 54
replace margin = .7643433 if V163001a == 55
replace margin = 45.76952 if V163001a == 56


***************************
** INDEPENDENT VARIABLES **
***************************

* Time

gen surveydate = date(V164004,"YMD")

format %tdMon_DD,_CCYY surveydate

gen edate = 20161108

gen electiondate=date(string(edate,"%8.0f"),"YMD")

format %tdMon_DD,_CCYY electiondate

gen time = electiondate - surveydate

* Gender

gen male = V161342

recode male (-9=.)
recode male (3=.)

recode male (2=0)

* Age

gen age = V161267

recode age (-9=.)
recode age (-8=.)

* Education

gen education = V161270

recode education (-9=.)
recode education (90=.)
recode education (95=.)

replace education = 1 if education > 0 & education < 9
replace education = 2 if education == 9
replace education = 3 if education > 9 & education < 13
replace education = 4 if education == 13
replace education = 5 if education > 13 & education < 17

* Interest for election

gen interest = 4 - V161004

* Caring about outcome (national)

gen care_whole = V161145

recode care_whole (-9=.)
recode care_whole (-8=.)

replace care_whole = 6 - care_whole

* Closeness of election (national)

gen closeness_whole = V161147

recode closeness_whole (-9=.)
recode closeness_whole (-8=.)
recode closeness_whole (-1=.)

recode closeness_whole (1=0)
recode closeness_whole (2=1)

* Closeness of election (state)

gen closeness_state = V161149

recode closeness_state (-9=.)
recode closeness_state (-8=.)
recode closeness_state (-1=.)

recode closeness_state (1=0)
recode closeness_state (2=1)

* Party identification scale (national)

gen pidscale = V161158x

recode pidscale (-9=.)
recode pidscale (-8=.)

gen pidscale_whole = pidscale

label define pidscale 1 "Strong loser PID" 2 "Weak loser PID" 3 "Leaner loser PID" 4 "No PID" 5 "Leaner winner PID" 6 "Weak winner PID" 7 "Strong winner PID"
label values pidscale_whole pidscale

* Party identification scale (state)

gen pidscale_state = pidscale if statewinner == 2
replace pidscale_state = 8 - pidscale if statewinner == 1

label values pidscale_state pidscale

* Political news attentiveness

gen news = V161009

recode news (-9=.)
recode news (-1=.)

replace news = 6 - news

replace news = 1 if V161008 == 0

* Joe Biden - Correct answer (U.S. vice-president) = 1, Incorrect answer = 0

gen biden = V162072

recode biden (-7=.)
recode biden (-6=.)

* Paul Ryan - Correct answer (Speaker of the House of Representatives) = 1, Incorrect answer = 0

gen ryan = V162073a

recode ryan (-7=.)
recode ryan (-6=.)

* Angela Merkel - Correct answer (German Chancellor) = 1, Incorrect answer = 0

gen merkel = V162074a

recode merkel (-7=.)
recode merkel (-6=.)

* Vladimir Putin - Correct answer (Russian President) = 1, Incorrect answer = 0

gen putin = V162075a

recode putin (-7=.)
recode putin (-6=.)

* John Roberts - Correct answer (Supreme Court Chief Justice) = 1, Incorrect answer = 0

gen roberts = V162076b

recode roberts (-7=.)
recode roberts (-6=.)

* Party with the most members in the U.S. House before election - Correct answer (Republican) = 1, Incorrect answer = 0

gen house = V161515

recode house (-9=.)
recode house (-5=.)

recode house (1=0)
recode house (2=1)

* Party with the most members in the U.S. Senate before election - Correct answer (Republican) = 1, Incorrect answer = 0

gen senate = V161516

recode senate (-9=.)
recode senate (-5=.)

recode senate (1=0)
recode senate (2=1)

* Political knowledge index

gen knowledge = ryan + biden + merkel + putin + roberts + house + senate

* Political discussion

gen discussion = V162174a

recode discussion (-9=.)
recode discussion (-8=.)
recode discussion (-7=.)
recode discussion (-6=.)
recode discussion (-1=.)

replace discussion = 0 if V162174 == 2

gen discussion_3pts = 0 if discussion < 2 & discussion!=.
replace discussion_3pts = 1 if discussion >= 2 & discussion < 4 & discussion!=.
replace discussion_3pts = 2 if discussion >= 4 & discussion!=.


*********************************
** NATIONAL-LEVEL EXPECTATIONS **
*********************************

* Forecasts (1 = Democrat, 2 = Republican, 5 = other)

gen forecast_whole = V161146

recode forecast_whole (-9=.)

* Identify correct forecasts

gen correct_whole_d = 0 if forecast_whole!=2 & forecast_whole!=.
replace correct_whole_d = 1 if forecast_whole == 2


******************************
** STATE-LEVEL EXPECTATIONS **
******************************

* Forecasts (1 = Democrat, 2 = Republican, 3 = other)

gen forecast_state = V161148

recode forecast_state (-9=.)

replace forecast_state = 3 if forecast_state > 2

* Identify correct forecasts

gen correct_state_d = 0 if forecast_state!=statewinner & forecast_state!=.
replace correct_state_d = 1 if forecast_state == statewinner & forecast_state!=.


**********
** SAVE **
**********

save "us2016_anes.dta", replace


******************************
** MAIN ANALYSES (NATIONAL) **
******************************

* Regression analysis and percentage of correct and incorrect forecasts (national)

logistic correct_whole_d discussion pidscale_whole interest news knowledge care_whole i.closeness_whole age i.male education time
estimates table, star(.05 .01 .001)

tab correct_whole_d if e(sample) == 1


***********************************
** SUMMARY STATISTICS (NATIONAL) **
***********************************

label variable correct_whole_d "Presidency winner (DV)"
label variable correct_state_d "State winner (DV)"
label variable discussion "Discussion"
label variable pidscale_whole "National PID"
label variable pidscale_state "State PID"
label variable interest "Interest"
label variable news "News attentiveness"
label variable knowledge "Political knowledge"
label variable care_whole "Caring about outcome"
label variable closeness_whole "Perceived closeness"
label variable closeness_state "Perceived closeness"
label variable age "Age"
label variable male "Gender (male = 1)"
label variable education "Education"
label variable time "Time of interview"
label variable margin "Margin"

estpost tabstat correct_whole_d discussion pidscale_whole interest news knowledge care_whole closeness_whole age male education time if e(sample) == 1, statistics(n mean p50 sd min max) columns(statistics)

esttab using "us2016_anes_summary_national.tex", substitute("\begin{table}[htbp]" "\begin{table}[H]") cells("count(label(N) fmt(%9.0fc)) mean(fmt(%5.2f) label(Mean)) p50(fmt(%5.1f) label(Median)) sd(fmt(%5.1f) label(Std. dev.)) min(fmt(%5.1f) label(Min)) max(fmt(%5.1f) label(Max))") label width(\textwidth) nomtitle nonumber noobs booktabs title("Summary statistics -- 2016 American presidential election (ANES), national level") replace

eststo clear


***************************
** MAIN ANALYSES (STATE) **
***************************

* Regression analysis and percentage of correct and incorrect forecasts (state)

melogit correct_state_d discussion pidscale_state interest news knowledge i.closeness_state age i.male education time margin || V163001a: , or
estimates table, star(.05 .01 .001)

tab correct_state_d if e(sample) == 1


********************************
** SUMMARY STATISTICS (STATE) **
********************************

label variable correct_whole_d "Presidency winner (DV)"
label variable correct_state_d "State winner (DV)"
label variable discussion "Discussion"
label variable pidscale_whole "National PID"
label variable pidscale_state "State PID"
label variable interest "Interest"
label variable news "News attentiveness"
label variable knowledge "Political knowledge"
label variable care_whole "Caring about outcome"
label variable closeness_whole "Perceived closeness"
label variable closeness_state "Perceived closeness"
label variable age "Age"
label variable male "Gender (male = 1)"
label variable education "Education"
label variable time "Time of interview"
label variable margin "Margin"

estpost tabstat correct_state_d discussion pidscale_state interest news knowledge closeness_state age male education time if e(sample) == 1, statistics(n mean p50 sd min max) columns(statistics)

esttab using "us2016_anes_summary_state.tex", substitute("\begin{table}[htbp]" "\begin{table}[H]") cells("count(label(N) fmt(%9.0fc)) mean(fmt(%5.2f) label(Mean)) p50(fmt(%5.1f) label(Median)) sd(fmt(%5.1f) label(Std. dev.)) min(fmt(%5.1f) label(Min)) max(fmt(%5.1f) label(Max))") label width(\textwidth) nomtitle nonumber noobs booktabs title("Summary statistics -- 2016 American presidential election (ANES), state level") replace

eststo clear

log close